package com.example.controller;

import com.example.entity.Building;
import com.example.entity.Page.PageRequest;
import com.example.result.CommonResult;
import com.example.service.BuildingService;
import com.example.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @project: smart-property
 * @description: 楼栋管理 controller 层
 * @author: cjq
 * @create: 2022-11-17 15:22
 **/
@RestController
@RequestMapping("/building")
public class BuildingController {
    @Autowired
    BuildingService buildingService;

    Integer cid;

    @Autowired
    RedisUtil redisUtil;


    /**
     * 小区楼座查重
     * @param cid
     * @param building
     * @return
     */
    @GetMapping("/listBuildingName")
    public CommonResult listBuildingName(Integer cid,String building){
        this.cid = cid;
        return CommonResult.ok(buildingService.listBuildingName(cid,building));
    }

    /**
     * 添加/修改楼栋信息
     * @param building
     * @return
     */
    @PostMapping("/saveBuildingInfo")
    public CommonResult saveBuildingInfo(@RequestBody Building building){
        String houseInfo ="houseInfo_"+this.cid;
        redisUtil.del(houseInfo);
        return CommonResult.ok(buildingService.saveBuildingInfo(building));
    }

    /**
     * 楼栋信息列表展示
     * @param purpose
     * @param building
     * @param pageRequest
     * @return
     */
    @GetMapping("/queryBuildingInfo")
    public CommonResult queryBuildingInfo(String purpose, String building, Integer cid,PageRequest pageRequest){
        return CommonResult.ok(buildingService.queryBuildingInfo(purpose,building,cid,pageRequest));
    }

    /**
     * 删除楼栋信息
     * @param id
     * @return
     */
    @DeleteMapping("/delBuildingInfo")
    public CommonResult delBuildingInfo(Integer id){
        String houseInfo ="houseInfo_"+this.cid;
        redisUtil.del(houseInfo);
        return CommonResult.ok(buildingService.delBuildingInfo(id));
    }

    /**
     * 批量删除楼栋信息
     * @param ids
     * @return
     */
    @PutMapping("/batchDel/{ids}")
    public CommonResult batchDel(@PathVariable Integer[] ids){
        String houseInfo ="houseInfo_"+this.cid;
        redisUtil.del(houseInfo);
        return CommonResult.ok(buildingService.batchDel(ids));
    }
}
